<script>
import { getSystemInfo } from "@/utils/utils";
import { getRegeoEvent } from "@/utils/address.js";
export default {
  globalData: {
    pdTop: 44, // 默认安全区域高度
    imageBaseUrl: "https://api.f4.huoke999.com",
  },
  onLaunch: function () {
    console.log("App Launch");
    const { headerSafe } = getSystemInfo();
    this.globalData.pdTop = headerSafe || 44;

    // 获取小程序位置信息
    // #ifdef MP
    this.getLocation();
    // #endif

    // 判断是否登录
    this.isLogin();
  },
  onShow: function () {
    console.log("App Show");
  },
  onHide: function () {
    console.log("App Hide");
  },
  methods: {
    isLogin() {
      // 读取本地缓存
      const userInfo = uni.getStorageSync("userInfo") || null;
      if (userInfo && Object.keys(userInfo).length) {
        const token = uni.getStorageSync("token") || "";
        // 写入全局store
        if (this.$store && this.$store.commit) {
          this.$store.commit("SET_USER_INFO", userInfo);
          this.$store.commit("SET_TOKEN", token);
        }
      }
    },
    async getLocation() {
      try {
        // 1. 检查设置
        const setting = await uni.getSetting();
        if (!setting.authSetting["scope.userFuzzyLocation"]) {
          // 2. 申请授权
          await uni.authorize({
            scope: "scope.userFuzzyLocation",
          });
        }
        // 3. 获取位置
        const result = await new Promise((resolve, reject) => {
          wx.getFuzzyLocation({
            type: "wgs84", // 坐标系类型：wgs84 或 gcj02
            success: resolve,
            fail: reject,
          });
        });
        if (result.errMsg === "getFuzzyLocation:ok") {
          const location = `${result.longitude},${result.latitude}`;
          getRegeoEvent(location);
        }
      } catch (error) {
        console.log("获取位置失败:", error);
        // 如果用户拒绝授权或获取位置失败，不影响应用正常使用
      }
    },
  },
};
</script>

<style lang="scss">
@import "uview-ui/index.scss";
@import url("static/font.css");
/*每个页面公共css */
</style>
